`timescale 1ns / 1ps

module board_test(
    input wire sys_clk,          // 系统时钟(假设25MHz)
    input wire [3:0] btn,        // 四位独立按键(未按高电平，按下低电平)
    input wire [1:0] sw,         // 两位拨动开关(控制RGB灯点亮)
    output reg [3:0] led,        // 四位独立LED灯
    output reg [2:0] rgbled0,    // RGB三色LED灯1
    output reg [2:0] rgbled1     // RGB三色LED灯2
);
    
    // -------------------------- 寄存器与变量定义 --------------------------
    // 计时相关
    reg [31:0] time_cnt;         // 时钟计数器(用于1秒计时)
    reg sec_pulse;               // 1秒脉冲标志(每1秒置1一次)
    
    // RGB灯相关
    reg [2:0] color_idx;         // 颜色索引(0-6循环)
    
    // LED模式相关
    reg [1:0] led_mode;          // LED模式: 0-全亮, 1-全灭, 2-流水灯, 3-闪烁
    reg [3:0] shift_reg;         // 流水灯移位寄存器
    reg [3:0] blink_reg;         // 闪烁状态寄存器
    
    // 按键消抖与边沿检测相关
    reg [18:0] btn_cnt [3:0];    // 按键消抖计数器(19位,对应20ms@25MHz)
    reg [3:0] btn_stable;        // 消抖后的稳定按键状态(1-未按,0-按下)
    reg [3:0] btn_prev;          // 上一周期的稳定按键状态
    reg [3:0] btn_pressed;       // 按键被按下的临时标志
    reg [3:0] btn_complete;      // 按键完整动作标志(按下→松开后置1)
    
    // 循环变量声明(模块级)
    integer i;  
    reg [2:0] color;

    // -------------------------- 初始化 --------------------------
    initial begin 
        led <= 4'b1111;
        time_cnt <= 32'd0;
        sec_pulse <= 1'b0;
        color_idx <= 3'd0;
        led_mode <= 2'd0;         // 初始模式: 全亮
        shift_reg <= 4'b0001;
        blink_reg <= 4'b1111;     // 初始化闪烁寄存器
        rgbled0 <= 3'b000;
        rgbled1 <= 3'b000;
        
        btn_stable <= 4'b1111;
        btn_prev <= 4'b1111;
        btn_pressed <= 4'b0000;
        btn_complete <= 4'b0000;
        i <= 0;
    end


    // -------------------------- 1秒计时逻辑 --------------------------
    always @(posedge sys_clk) begin
        if (time_cnt >= 32'd124_999_999) begin
            time_cnt <= 32'd0;
            sec_pulse <= 1'b1;
        end else begin
            time_cnt <= time_cnt + 32'd1;
            sec_pulse <= 1'b0;
        end
    end


    // -------------------------- RGB灯颜色切换 --------------------------
    always @(posedge sys_clk) begin
        if (sec_pulse) begin
            color_idx <= (color_idx >= 3'd6) ? 3'd0 : color_idx + 3'd1;
        end
    end

    always @(*) begin

        case (color_idx)
            3'd0: color = 3'b100;  // 红色
            3'd1: color = 3'b010;  // 绿色
            3'd2: color = 3'b001;  // 蓝色
            3'd3: color = 3'b110;  // 黄色
            3'd4: color = 3'b101;  // 紫色
            3'd5: color = 3'b011;  // 青色
            3'd6: color = 3'b111;  // 白色
            default: color = 3'b000;
        endcase
        rgbled0 = sw[0] ? color : 3'b000;
        rgbled1 = sw[1] ? color : 3'b000;
    end


    // -------------------------- 按键消抖逻辑 --------------------------
    always @(posedge sys_clk) begin
        for (i = 0; i < 4; i = i + 1) begin
            if (btn[i] != btn_stable[i]) begin
                if (btn_cnt[i] >= 19'd499_999) begin
                    btn_stable[i] <= btn[i];
                    btn_cnt[i] <= 19'd0;
                end else begin
                    btn_cnt[i] <= btn_cnt[i] + 19'd1;
                end
            end else begin
                btn_cnt[i] <= 19'd0;
            end
        end
    end


    // -------------------------- 按键完整动作检测 --------------------------
    always @(posedge sys_clk) begin
        btn_prev <= btn_stable;
    end

    always @(posedge sys_clk) begin
        for (i = 0; i < 4; i = i + 1) begin
            if (btn_prev[i] == 1'b1 && btn_stable[i] == 1'b0) begin
                btn_pressed[i] <= 1'b1;
            end else if (btn_prev[i] == 1'b0 && btn_stable[i] == 1'b1 && btn_pressed[i]) begin
                btn_complete[i] <= 1'b1;
                btn_pressed[i] <= 1'b0;
            end else begin
                btn_complete[i] <= 1'b0;
            end
        end
    end


    // -------------------------- LED模式切换 --------------------------
    // 仅处理模式切换，不直接修改shift_reg/blink_reg
    always @(posedge sys_clk) begin
        if (btn_complete[0]) begin
            led_mode <= 2'd0;
        end else if (btn_complete[1]) begin
            led_mode <= 2'd1;
        end else if (btn_complete[2]) begin
            led_mode <= 2'd2;
        end else if (btn_complete[3]) begin
            led_mode <= 2'd3;
        end
    end


    // -------------------------- 流水灯逻辑（合并shift_reg赋值） --------------------------
    always @(posedge sys_clk) begin
        if (btn_complete[2]) begin  // 切换到流水灯模式时重置
            shift_reg <= 4'b0001;
        end else if (sec_pulse && led_mode == 2'd2) begin  // 流水灯模式下移位
            shift_reg <= {shift_reg[2:0], shift_reg[3]};
        end
    end


    // -------------------------- 闪烁逻辑（合并blink_reg赋值，解决多驱动问题） --------------------------
    always @(posedge sys_clk) begin
        if (btn_complete[3]) begin  // 切换到闪烁模式时重置
            blink_reg <= 4'b1111;
        end else if (sec_pulse && led_mode == 2'd3) begin  // 闪烁模式下翻转
            blink_reg <= ~blink_reg;
        end
    end


    // -------------------------- LED输出控制 --------------------------
    always @(*) begin
        case (led_mode)
            2'd0: led = 4'b1111;      // 全亮
            2'd1: led = 4'b0000;      // 全灭
            2'd2: led = shift_reg;    // 流水灯
            2'd3: led = blink_reg;    // 闪烁
            default: led = 4'b1111;
        endcase
    end

endmodule